package com.bit.librarymanagementsystem.Controller;


import com.bit.librarymanagementsystem.Constant.Constant;
import com.bit.librarymanagementsystem.Service.UserService;
import com.bit.librarymanagementsystem.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    
    @RequestMapping("/login")
    public boolean login(String name, String password, HttpSession session){
        if(!StringUtils.hasLength(name)||!StringUtils.hasLength(password)){//如果为空或者为null就返回false
            return false;
        }
//        if ("admin".equals(name)&&"admin".equals(password)){//没有连数据库,先写死
//            return true;
//        }
        UserInfo userInfo= userService.queryUserByName(name);
        if (userInfo==null){
            return false;
        }
        //校验密码是否正确
        if(userInfo!=null&&password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute(Constant.SESSION_LOGIN_USER_NAME,userInfo.getUserName());
            return true;
        }

        return false;
    }



}
